package org.hhchat.leetcode.array;

import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.Set;

/**
 * Created this one by HMH on 2017/8/30.
 * https://leetcode.com/problems/third-maximum-number/description/
 */
public class code414 {
    static class Solution {
        public int thirdMax(int[] nums) {
            //todo read the realization
            PriorityQueue<Integer> queue = new PriorityQueue<>();
            Set<Integer> set = new HashSet<>();
            for (int num : nums) {
                if (!set.contains(num)) {
                    queue.offer(num);
                    set.add(num);
                    if (queue.size() > 3) {
                        set.remove(queue.poll());
                    }
                }
            }
            if (queue.size() < 3) {
                while(queue.size()>1){
                    queue.poll();
                }
            }
            return queue.peek();
        }
    }

    public static void main(String[] args){

    }
}
